<template>
  <div class="app-container">
    <!-- 添加或修改就诊信息对话框 -->
      <el-tabs type="border-card" v-model="activeName"  @tab-click="handleClick">
        <el-tab-pane name="1" label="基本信息">
          <el-form ref="patientForm" :rules="patientrules" :model="patientForm" label-width="100px" label-position="top">
            <el-row :gutter="10">
              <el-row :gutter="10">
                <el-col :span="10">
                  <el-form-item label="身份证号" prop="patientIdentityNo">
                    <el-input v-model="patientForm.patientIdentityNo" @blur="identityInfo" placeholder="请输入患者身份证号"  maxlength="18"/>
                  </el-form-item>
                </el-col>
                <el-col :span="2">
                  <span ref="checkPatient"></span>
                </el-col>
<!--                <el-col :span="8" :offset="4">-->
<!--                  <el-form-item label="患者编号(只读)" prop="patientNo">-->
<!--                    <el-input v-model="patientForm.patientNo" placeholder="" readonly/>-->
<!--                  </el-form-item>-->
<!--                </el-col>-->
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="患者姓名" prop="patientName">
                    <el-input v-model="patientForm.patientName" placeholder="请输入患者名称"  maxlength="10"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="患者生日" prop="patientBirthday">
                    <el-date-picker clearable
                                    v-model="patientForm.patientBirthday"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择患者生日"
                                     >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="患者年龄" prop="patientAge">
                    <el-input v-model="patientForm.patientAge"  placeholder="请输入患者年龄"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="患者性别" prop="patientGender">
                    <el-select v-model="patientForm.patientGender" placeholder="请选择患者性别"  clearable>
                      <el-option
                        v-for="dict in dict.type.sys_user_sex"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select>
                    <!--                    <el-input v-model="patientForm.patientGender" placeholder="请输入患者性别"/>-->
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="患者身高" prop="patientHeight">
                    <el-input v-model="patientForm.patientHeight" placeholder="请输入患者身高"  maxlength="3">
                      <i slot="append">cm</i>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="患者体重" prop="patientWeight">
                    <el-input v-model="patientForm.patientWeight" placeholder="请输入患者体重"  maxlength="3">
                      <i slot="append">kg</i>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="患者民族" prop="patientFolk">
                    <el-input v-model="patientForm.patientFolk" placeholder="请输入患者民族"  maxlength="10"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="患者职业" prop="patientJob">
                    <el-input v-model="patientForm.patientJob" placeholder="请输入患者职业"  maxlength="10"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="婚姻状态" prop="patientMarriage">
                    <el-select v-model="patientForm.patientMarriage" placeholder="请选择患者状态" clearable>
                      <el-option
                        v-for="dict in dict.type.marital_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select>
                    <!--                    <el-input v-model="patientForm.patientMarriage" placeholder="请输入婚姻状态"/>-->
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="患者地址" prop="patientAddress">
                    <el-input v-model="patientForm.patientAddress" placeholder="请输入患者地址"  maxlength="30"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :offset="4">
                  <el-form-item label="联系方式" prop="patientPhone">
                    <el-input v-model="patientForm.patientPhone" placeholder="请输入联系方式"  maxlength="11"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-row>
          </el-form>
        </el-tab-pane>
        <div style="float: right">
          <el-button type="primary" @click="submitForm">提 交</el-button>
          <el-button @click="cancel">返 回</el-button>
        </div>
      </el-tabs>

      <div slot="footer" class="dialog-footer">

      </div>
  </div>
</template>

<script>
import {listPatient, getPatient, delPatient, addPatient, updatePatient, insertPatient} from "@/api/gaoxin/patient";
import Recorder from "js-audio-recorder";
import {addClinic, updateClinic} from "@/api/gaoxin/clinic";
import {getUserAll} from "@/api/system/user";
import OcrAsr from "@/components/OcrAsr";
import idCode from "@/plugins/idCode";


export default {
  name: "Patient",
  dicts: ['patient_status','sys_user_sex','marital_status'],
  data() {
    var validateName = (rule, value, callback) => {
      let patientIdentityNo = value;
      let total = 0;
      listPatient({patientIdentityNo: patientIdentityNo}).then(response => {
        total = response.total;
        if (total != 0) {
          callback(new Error('该患者已存在'));
        } else {
          callback();
        }
      })
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 患者查询参数
      queryPatientParams: {
        pageNum: 1,
        pageSize: 10,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      },
      // 表单参数
      patientForm: {
        patientBirthday: ''
      },
      //----弹窗----
      // pane
      activeName: "1",
      //患者下的任务是否都已审核
      isActive:true,
      //----弹窗----

      // 表单校验
      patientrules: {
        patientName: [
          {required: true, message: "患者名称不能为空", trigger: "blur"},
        ],
        patientIdentityNo: [
          {required: true, message: '身份证不能为空', trigger: "blur"},
          {pattern:/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的身份证号', trigger: 'blur'},
        ],
        patientPhone: [
          {required: true, message: "患者联系方式不能为空", trigger: "blur"},
          {pattern:/^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/, message: "请输入正确的联系方式(固话加区号)", trigger: "blur"}
        ]
      },
    };
  },
  created() {
    // const path = this.$route.path;
    // const patientId = Number(path.split('/').slice(-1));
    // const query = {patientId: patientId};
    // listPatient(query).then(response => {
    //   this.patientForm = response.rows[0];
    // })
    // this.getList();
    this.getDicts("marital_status").then(res=>{
      this.marital_status=res.data;
    })
  },
  watch: {
    'patientForm.patientBirthday': {
      handler: function (newVal, oldVal) {
        console.log(newVal, oldVal);
        if(newVal != '' && newVal != null) {
          var birthdays = new Date(newVal.replace(/-/g, "/"));
          var d = new Date();
          var age =
            d.getFullYear() -
            birthdays.getFullYear() -
            (d.getMonth() < birthdays.getMonth() ||
            (d.getMonth() == birthdays.getMonth() &&
              d.getDate() < birthdays.getDate())
              ? 1
              : 0);
          this.patientForm.patientAge = age;
        }

      },
      deep: true
    },
    'patientForm.patientAddress': {
      handler(val) {
        console.log(val)
        this.patientForm.patientAddress = val;
      }
    },
    deep: true
  },
  methods: {
    getInfo() {
      getUserAll().then(response => {
        this.userOptions = response.users;
        console.log('userOptions',this.userOptions)
      });
    },
    // 取消按钮
    cancel() {
      this.$tab.closePage();
      this.$router.go(-1);
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: "0",
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["patientForm"].validate(valid => {
        if(valid){
          this.patientForm.patientStatus = '1';
          insertPatient(this.patientForm).then(response => {
            this.$modal.msgSuccess("操作成功");
          });
          this.$router.push("/patient/index");
        }
      });
    },
    //获取身份证信息: 当失去焦点后自动填写 生日、年龄、性别、所在省
    identityInfo() {
      let obj = idCode.createNew(this.patientForm.patientIdentityNo);
      if(obj.isCheck()) {
        this.patientForm.patientBirthday = `${obj.getYears()}-${obj.getMonth()}-${obj.getDay()}`;
        this.patientForm.patientGender = obj.getGender() == '男' ? '1' : '0';
        console.log(obj.getGender())
        console.log(this.patientForm.patientGender);
        this.patientForm.patientAddress = obj.getProvinceName();
      }
    },
    //----------------------------------------------------------
    //----------------------------------------------------------
    handleClick(tab, event) {
      console.log(tab, event);
    },
    //----------------------------------------------------------
    handleClose() {
    },
    // 关闭识别弹窗
    cancelDialog() {
      this.open0 = false;
      this.open1 = false;
    },
  }
};
</script>
